package cn.com.graPro.service;

import cn.com.graPro.utils.GsonUtils;
import cn.com.graPro.utils.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
public class ImgCensorService {

	/**
	 * AIP图像组合API
	 */

	public static String ImgCensorUrl(final String imageUrl, String param) {
		// 图像组合APIurl
		String imgCensorUrl = "https://aip.baidubce.com/api/v1/solution/direct/img_censor";
		// String filePath = "########本地图片文件#######";
		try {
			// 请求参数
			Map<String, Object> sceneConf = new HashMap<String, Object>();
			Map<String, Object> ocrConf = new HashMap<String, Object>();
			ocrConf.put("recognize_granularity", "big");
			ocrConf.put("language_type", "CHN_ENG");
			ocrConf.put("detect_direction", true);
			ocrConf.put("detect_language", true);
			sceneConf.put("ocr", ocrConf);

			Map<String, Object> input = new HashMap<String, Object>();
			List<Object> scenes = new ArrayList<Object>();
			scenes.add(param);

//			 scenes.add("ocr"); scenes.add("face"); scenes.add("public");
//			 scenes.add("antiporn"); scenes.add("terror");
//			 scenes.add("webimage"); scenes.add("disgust");
//			 scenes.add("watermark");


			// byte[] imgData = FileUtil.readFileByBytes(imageUrl);
			// String imgStr = Base64Util.encode(imgData);
			// String imageUrl = "########网络图片地址#######";
			input.put("imgUrl", imageUrl);// 与image二者选一
			input.put("scenes", scenes);
			input.put("sceneConf", sceneConf);

			String params = GsonUtils.toJson(input);
			System.out.println(params);
			/**
			 * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
			 */
			String accessToken = "24.c733e987f48798b653f1ef4634915481.2592000.1561351088.282335-15668388";
			String result = HttpUtil.post(imgCensorUrl, accessToken, "application/json", params);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
